<?php
function jxcompa_query($filepath)
{
    if (!file_exists($filepath) || file_get_contents($filepath) == '') {
        $filepath = $_SERVER['DOCUMENT_ROOT'] . '/files/sharingconfigure/drivenormalpz.afikconfig';
    }
    $text = file_get_contents($filepath);
    $lines = explode("\n", $text);
    $parsedCommands = [];
    foreach ($lines as $line) {
        $line = trim($line); 
        if ($line === '') { 
            continue;
        }
        if (substr($line, -1) === ';') {
            $line = substr($line, 0, -1); 
            if (preg_match('/^(\w+)\->(.+)$/', $line, $matches)) {
                $parsedCommands[$matches[1]] = $matches[2];
            } else {
                die("The configuration file is abnormal");
            }
        } else {
            die("The configuration file is abnormal");
        }
    }
    return $parsedCommands;
}
?>
<?php
$allarray_filesharing = jxcompa_query($_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $newremainpath . '/drivenormalpz.afikconfig');
if ($allarray_filesharing['password'] == 'null' || !isset($allarray_filesharing['password'])) {
    include($_SERVER['DOCUMENT_ROOT'] . '/share/share.php');
    exit;
} else {
    $authpassword  = $allarray_filesharing['password'];
}
if ($allarray_filesharing['bkg1'] == 'null' || !isset($allarray_filesharing['bkg1'])) {
    $bkg1_auth = "/ast/photo/background1.jpeg";
} else {
    $bkg1_auth = $allarray_filesharing['bkg1'];
}
if ($allarray_filesharing['head'] == 'null' || !isset($allarray_filesharing['head'])) {
    $headpot = "CloudsumionDisk";
} else {
    $headpot = $allarray_filesharing['head'];
}
ini_set('session.gc_maxlifetime', 3600); 
session_set_cookie_params([
  'lifetime' => 3600,
  'path' => '/',
  'domain' => $_SERVER['HTTP_HOST'],
  'secure' => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
  'httponly' => true,
]);
$getauth_id = 'cloudsumion_' . $newremainpath;
if ($newremainpath2 == '' || $newremainpath2 == null) {
    $newremainpath2 = $_POST['auth_account'];
}
$getauth_time = $getauth_id . '_valid_until';
session_start();
function checkauthcode($authCode)
{
    global $authpassword;
    $correctAuthCode = $authpassword;
    return $authCode === $correctAuthCode;
}
if (!isset($_SESSION[$getauth_id]) || !checkauthcode($_SESSION[$getauth_id]) || time() > $_SESSION[$getauth_time]) {
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['auth_code'])) {
        $authCode = trim($_POST['auth_code']);
        if (checkauthcode($authCode)) {
            $_SESSION[$getauth_id] = $authCode;
            $_SESSION[$getauth_time] = time() + 3600;
            $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
            header("Location: " . $protocol . "://" . $_SERVER['HTTP_HOST'] . '/share/' . $newremainpath2);
            exit();
        } else {
            $error = "The sharing code is incorrect";
        }
    }
?>
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?php echo $headpot;?></title>
        <link rel="stylesheet" href="/ast/share.css">
    </head>
    <style>
        body {
            background-image: url(<?php echo $bkg1_auth ?>);
        }
    </style>

    <body>
        <div class="auth-form">
            <h1>Verification</h1>
            <form method="POST">
                <input type="text" name="auth_code" placeholder="Please enter the sharing code" required>
                <input value="<?php echo $newremainpath2 ?>" type="text" style="display: none;" name="auth_account" required>
                <input type="submit" value="Determine">
            </form>
            <?php if (isset($error)): ?>
                <div class="error"><?php echo $error; ?></div>
            <?php endif; ?>
        </div>
    </body>

    </html>
<?php
} else {
    session_write_close();
    ob_clean();
    include($_SERVER['DOCUMENT_ROOT'] . '/share/share.php');
}
?>